package com.abe.libquick.widget;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;

public abstract class LimitLengthListener implements TextWatcher {
    private int length = Integer.MAX_VALUE;

    public LimitLengthListener() {
    }

    public LimitLengthListener(int length) {
        this.length = length;
    }

    public abstract void onData(String value, String newValue);

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String value = s.toString();
        if (TextUtils.isEmpty(value)) return;
        if (value.length() > length) {
            onData(value, value.substring(0, length));
        }
    }
}